package com.common.UITags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * dropdown下拉数据
 * @author zhanglong
 *
 */
public class DataTag extends TagSupport{

	private static final long serialVersionUID = 1L;
	StringBuffer sBF = new StringBuffer();
	
	//下拉列表的显示值
	private String key;
	//下拉列表的实际值
	private String value;

	@Override
	public int doStartTag() throws JspException {
		try{
			String defaultselect;
			JspWriter out = pageContext.getOut();
			DropDownTag dropdown = (DropDownTag)getParent();
			defaultselect = dropdown.getDefaultselect();
			sBF.setLength(0);
			if((key!=null && !"".equals(key))&&(value!=null && !"".equals(value))){
				if(key.equals(defaultselect)){
					sBF.append(" <option value=\"" + key +"\" " );
					sBF.append(" selected=\"selected\" >");
				}else{
					sBF.append(" <option value=\"" + key + "\"> ");
				}
				sBF.append(value);
				sBF.append(" </option> ");
			}
			
			out.print(sBF.toString());
		}catch(IOException e){
			throw new JspException("标签dropdown往前台写入时异常！");
		}
		catch(Exception e){
			throw new JspException("标签内部异常！");
		}
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	
	//get 和 set方法
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	
	
}
